// pages/mycollection/mycollection.js、
import Toast from '../../miniprogram_npm/_vant-weapp@0.5.7@vant-weapp/toast/toast';
import Dialog from '../../miniprogram_npm/_vant-weapp@0.5.7@vant-weapp/dialog/dialog';
//收藏列表 收藏删除
import { getFavorList, setDeleFavor} from '../../config/https'

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  
  data: {
    vipType:'',       //1是vip0不是
    page:0,
    loadMore: false, //加载中提示
    finished: false, //是否已经请求完全部数据
    list:[],     //收藏数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    wx.getStorage({
      key: 'vipStast',
      success: function (res) {
        console.log(res)
        that.setData({
          vipType: res.data
        })
      },
    })
    this.onList()
  },

  onList(){
    var that = this
    that.setData({
      loadMore:true
    })
    getFavorList({
      page:++that.data.page,
    }).then(res=>{
      console.log(res.list)
      var list = res.list
      list = that.data.list.concat(list)
      that.setData({
        loadMore: false
      })
      if (list.length == 0) {
      } else {
        that.setData({
          finished: res.list.length < res.pagesize ? true : false
        })
      }
      wx.stopPullDownRefresh();
      that.setData({
        list
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  //点击跳转到首页
  onIndex(){
    wx.reLaunch({
      url: '/pages/tapswitching/tapswitching',
    })
  },
  //点击跳转详情页
  onDetail(e) {
    const {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/Directsellingto/Directsellingto?id=' + id,
    })
  },
  // 点击删除
  onDel(e){
    var index = e.currentTarget.dataset.index
    let list = this.data.list
    Dialog.confirm({
      message: '确定删除当前选择的商品吗?',
    }).then(() => {
      let id = new Object
      console.log(list)
      id[0] = list[index].id
      setDeleFavor({
        ids: id
      }).then(res=>{
        Toast("删除成功~~")
        list.splice(index, 1)
        this.setData({ list })
      })
      
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.finished && !this.data.loadMore) {
      this.onList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
   
  }
})